/*
 * 整个编辑页面的入口
 */
define(function(require, exports, module) {

	/*实例化相册编辑的所有的入口*/
	exports.init = function() {
		require('form-validator');
		require('tree');
		ajax_skip();
		init_page();
		test();
	};

	function ajax_skip() {
		/*获取到需要点击的a标签，并且是更新内容区的标签*/
		$('body').on('click', "a[form-type],button[form-type]", function() {
			var refresh_container = $(this).attr('form-type');
			//判断是不是表单操作
			if (refresh_container.indexOf("form-") == 0) {
				return form_type_form($(this));
			} else if (refresh_container.indexOf("none-") == 0) {
				return form_type_none($(this));
			} else if (refresh_container.indexOf("alert-") == 0) {
				return form_type_alert($(this));
			} else if (refresh_container.indexOf("page-") == 0) {
				return form_type_page_skip($(this));
			} else {
				return form_type_refresh($(this));
			}
		});

		/*监听文件导入功能*/
		$(document).on('change', "input[type='file'][form-type]", function(e) {
			var refresh_container = $(this).attr('form-type');
			if (refresh_container.indexOf("uplaoder-") == 0) {
				var $id = refresh_container.split('-')[1];
				/*获取文件名*/
				var path_name = e.srcElement.value;
				var arr = path_name.split('\\');
				var file_name = arr[arr.length - 1];
				var obj = $("#modal-body");
				obj.html(file_name);
				/*$(this).nextAll('span.filename').text(file_name);
				$(this).nextAll('span.action').text('已选图片');
				remove($(this));*/
			}
		});
	}

	/*提交类型为表单的ajax操作
	 *$this:当前点击对象
	 */
	function form_type_form($this) {
		var $form = $this.closest('form');
		var form_type = $this.attr('form-type');
		var refresh_container = form_type.split('-')[1];
		//验证表单
		if (!yun_form_validator($form)) {
			return false;
		}

		if (typeof($this.attr("close-form-table")) != "undefined") {
			$this.closest('.' + $this.attr("close-form-table")).modal('hide');
		}

		$$.ajax({
			success: function(data) {
				var $content = $('#' + refresh_container);
				$content.html('');
				$content.append(data);
				refresh(refresh_container);
			},
			'form': $form,
		});
	}

	/*提交类型为普通的页面刷新
	 *$this:当前点击对象
	 */

	function form_type_refresh($this) {
		var refresh_container = $this.attr('form-type');
		var method = 'post';
		if ($this.attr("form-method") != "undefined") {
			method = 'get'
		}

		if (typeof($this.attr("close-form-table")) != "undefined") {
			$this.closest('.' + $this.attr("close-form-table")).modal('hide');
		}

		$$.ajax({
			url: $this.attr('href'),
			data: {

			},
			type: method,
			success: function(data) {
				var $content = $('#' + refresh_container);
				$content.html('');
				$content.append(data);
				refresh(refresh_container);
			},
			error: function() {
				yun_common.hint.error('系统错误，请重试！');
			}
		});
		return false;
	}

	/*提交类型为无刷新
	 *$this:当前点击对象
	 */
	function form_type_none($this) {
		var refresh_container = $this.attr('form-type');
		var method = 'post';
		if ($this.attr("form-method") != "undefined") {
			method = 'get'
		}

		if (typeof($this.attr("close-form-table")) != "undefined") {
			$this.closest('.' + $this.attr("close-form-table")).modal('hide');
		}

		$$.ajax({
			url: $this.attr('href'),
			data: {

			},
			type: method,
			success: function(data) {
				console.log('无刷新提交');
			},
			error: function() {
				yun_common.hint.error('系统错误，请重试！');
			}
		});
		return false;
	}

	/*弹窗显示*/
	function form_type_alert($this) {
		var $url = $this.attr('url');
		var $id = $this.attr('href');

		var method = 'post';
		if ($(this).attr("form-method") != "undefined") {
			method = 'get'
		}

		$content = $($id);
		$content.html('');

		$$.ajax({
			url: $url,
			type: method,
			success: function(data) {
				$content.append(data);
			},
			error: function() {
				yun_common.hint.error('系统错误，请重试！');
			}
		});
	}


	function form_type_page_skip($this) {
		var page = $('#yun_page_skip_input').val();
		if (isNaN(page)) {
			alert('页码必须为整数');
			return false;
		}
		
		$$.ajax({
			url: $this.attr('href'),
			data: {
				page: page,
				table:'table',
			},
			type: 'get',
			success: function(data) {
				var $content = $('#table');
				$content.html('');
				$content.append(data);
				PUBLIC.matrix_refresh.execute_table();
			},
			error: function() {
				alert("页面加载错误");
			}
		});
		
		return false; 
	}

	/*分页*/
	function init_page() {
	
	}


	function test() {
		/*监听时间选择空间的删除按钮*/
		$("body").on('click', 'a.data-del', function() {
			$(this).closest('.y-datatime').find('input').val('');
			return false;
		});

		/*监听批量删除按钮*/
		var data_del = false; //是否点击的是删除按钮，如果是值为当前数据的id
		$("body").on('click', 'a.data-del-all', function() {
			var $this = $(this);
			//获取被选中的元素
			var $id_obj = $('#table tbody td span.checked input');
			//获取被选中的元素的值
			var id_arr = new Array();
			$id_obj.each(function() {
				id_arr.push($(this).attr('data'));
			});

			//console.log($id_obj);
			if (id_arr.length != 0) {
				var ids = id_arr.join(",");
				del_ajax($this.attr('href'), ids);
			} else {
				alert("还没有选择内容");
			}
			return false;
		});

		$("body").on('click', 'a.data-del-bean-alert', function() {
			del_ajax($(this).attr('href'), data_del);
		});

		$("body").on('click', 'a.data-del-bean', function() {
			data_del = $(this).attr('data');
		});
	};

	var refresh = function(type) {
		switch (type) {
			case 'content':
				PUBLIC.matrix_refresh.execute_content();
				break;
			case 'table':
				PUBLIC.matrix_refresh.execute_table();
				break;
			default:
				break;
		};
	};


	var del_ajax = function(url, ids) {
		$$.ajax({
			url: url,
			data: {
				id: ids
			},
			type: 'get',
			success: function(data) {
				var $content = $('#table');
				$content.html('');
				$content.append(data);
				PUBLIC.matrix_refresh.execute_table();
			},
		});
	}
});